package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Examine;
import com.ruoyi.system.domain.Statistics;
import com.ruoyi.system.domain.vo.FileUploadVO;
import com.ruoyi.system.mapper.ExamineMapper;
import com.ruoyi.system.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SubsidyApplicationMapper;
import com.ruoyi.system.domain.SubsidyApplication;
import com.ruoyi.system.service.ISubsidyApplicationService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 境外宣传营销补贴申请，存储补贴申请信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-22
 */
@Service
public class SubsidyApplicationServiceImpl extends ServiceImpl<SubsidyApplicationMapper, SubsidyApplication> implements ISubsidyApplicationService {
    @Autowired
    private SubsidyApplicationMapper subsidyApplicationMapper;

    @Resource
    private FileUploadService fileUploadService;


    @Resource
    private ExamineMapper examineMapper;


    /**
     * 查询境外宣传营销补贴申请，存储补贴申请信息
     *
     * @param id 境外宣传营销补贴申请，存储补贴申请信息主键
     * @return 境外宣传营销补贴申请，存储补贴申请信息
     */
    @Override
    public SubsidyApplication selectSubsidyApplicationById(Long id) {
        SubsidyApplication subsidyApplication = subsidyApplicationMapper.selectById(id);
        List<FileUploadVO> list = fileUploadService.listFileUploadsByBusinessId(subsidyApplication.getBusinessId());
        subsidyApplication.setFiles(list);
        List<Examine> examines = examineMapper.selectList(
                new QueryWrapper<Examine>().eq("business_id", subsidyApplication.getId()).orderByDesc("create_time")
        );
        examines.sort((e1, e2) -> e2.getCreateTime().compareTo(e1.getCreateTime()));
        subsidyApplication.setExamines(examines);
        return subsidyApplication;
    }

    /**
     * 查询境外宣传营销补贴申请，存储补贴申请信息列表
     *
     * @param subsidyApplication 境外宣传营销补贴申请，存储补贴申请信息
     * @return 境外宣传营销补贴申请，存储补贴申请信息
     */
    @Override
    public List<SubsidyApplication> selectSubsidyApplicationList(SubsidyApplication subsidyApplication) {


            startPage();
            QueryWrapper<SubsidyApplication> subsidyApplicationQueryWrapper = new QueryWrapper<>(subsidyApplication);
            List<SubsidyApplication> subsidyApplicationsList  = new ArrayList<>();
            subsidyApplicationQueryWrapper.orderByDesc("created_at");
            if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
                subsidyApplicationsList = subsidyApplicationMapper.selectList(subsidyApplicationQueryWrapper);
            } else {
                subsidyApplicationQueryWrapper.eq("create_by", SecurityUtils.getUserId().toString());
                subsidyApplicationsList = subsidyApplicationMapper.selectList(subsidyApplicationQueryWrapper);
            }
//            if(subsidyApplicationsList.size()>0){
//                subsidyApplicationsList.forEach(row -> {
//                    List<Examine> examines = row.getId()  == null ? new ArrayList<>() : examineMapper.selectList(new QueryWrapper<Examine>().eq("business_id", row.getId()).eq(StringUtils.isNotEmpty(row.getStatus()), "status", row.getStatus()).orderByDesc("create_time"));
//                    row.setExamines(examines);
//                });
//            }
            return subsidyApplicationsList;

    }

    /**
     * 新增境外宣传营销补贴申请，存储补贴申请信息
     *
     * @param subsidyApplication 境外宣传营销补贴申请，存储补贴申请信息
     * @return 结果
     */
    @Override
    public int insertSubsidyApplication(SubsidyApplication subsidyApplication) {
        subsidyApplication.setCreateBy(SecurityUtils.getUserId().toString());
        subsidyApplication.setStatus(0);

        int i = subsidyApplicationMapper.insert(subsidyApplication);
//         subsidyApplicationMapper.insertSubsidyApplication(subsidyApplication);
        System.out.println(subsidyApplication.getId());
        // 新增审核信息
        Examine examine = new Examine();
        examine.setBusinessId(String.valueOf(subsidyApplication.getId())).setStatus(0).setType(subsidyApplication.getEventType()).setCreateBy(SecurityUtils.getUserId().toString()).setCreateTime(DateUtils.getTime()).setRemark(subsidyApplication.getRemark());
        examineMapper.insert(examine);
        return i;
    }

    /**
     * 修改境外宣传营销补贴申请，存储补贴申请信息
     *
     * @param subsidyApplication 境外宣传营销补贴申请，存储补贴申请信息
     * @return 结果
     */
    @Override
    public int updateSubsidyApplication(SubsidyApplication subsidyApplication) {
        // 新增审核信息
        Examine examine = new Examine();
        examine.setBusinessId(String.valueOf(subsidyApplication.getId())).setStatus(subsidyApplication.getStatus()).setType(subsidyApplication.getEventType()).setCreateBy(SecurityUtils.getUserId().toString()).setCreateTime(DateUtils.getTime()).setRemark(subsidyApplication.getRemark());
        examineMapper.insert(examine);
        return subsidyApplicationMapper.updateById(subsidyApplication);
    }

    /**
     * 批量删除境外宣传营销补贴申请，存储补贴申请信息
     *
     * @param ids 需要删除的境外宣传营销补贴申请，存储补贴申请信息主键
     * @return 结果
     */
    @Override
    public int deleteSubsidyApplicationByIds(Long[] ids) {
        return subsidyApplicationMapper.deleteSubsidyApplicationByIds(ids);
    }

    /**
     * 删除境外宣传营销补贴申请，存储补贴申请信息信息
     *
     * @param id 境外宣传营销补贴申请，存储补贴申请信息主键
     * @return 结果
     */
    @Override
    public int deleteSubsidyApplicationById(Long id) {
        return subsidyApplicationMapper.deleteSubsidyApplicationById(id);
    }
}
